# coding=utf-8
import cv2
import dlib
import numpy as np
import os,glob
from skimage import io


detector =dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('model/shape_predictor_68_face_landmarks.dat')
face_rec = dlib.face_recognition_model_v1("model/dlib_face_recognition_resnet_model_v1.dat")


face_descriptors =[]

#1.检测人脸库中人脸
def load_face_lib():
    print("检测人脸库中人脸")
    # 对文件夹下的每一张图片进行:
    #1.人脸检测
    # 2.关键点检测
    # 3.描述子提取

    # 候选人脸文件夹
    faces_folder_path = "face_lib/"
    # 候选人脸描述子
    listdescriptors =[]
    # print("开始处理候选人脸库中人脸......")
    face_lib_files =glob.glob(os.path.join(faces_folder_path,"*.jpg"))
    for face_img_path in face_lib_files:
        # print("正在处理文件:{}".format(face_img_path))
        img = io.imread(face_img_path)
        # 1.人脸检测
        faces = detector(img,1)
        # print("检测到人脸数量:{}".format(len(faces)))

        for i, face in enumerate(faces):
            # 2.关键点检测
            shape =predictor(img ,face) #提取人脸68个特征点
            # 3.描述子提取。128D/向量
            faced_descriptor = face_rec.compute_face_descriptor(img, shape)
            # 转换为numpy array
            v=np.array(faced_descriptor)
            face_descriptors.append(v)
            # print(faced_descriptor)

    # 准备好数据集
    # 也就是给图片打标签，监督学习
    labels = ['王俊凯','杨洋','吴才朋','陈伟霆','赵丽颖']

    return face_descriptors,labels
